package com.zxy.furns.dao.impl;

import com.zxy.furns.dao.BasicDAO;
import com.zxy.furns.dao.FurnDAO;
import com.zxy.furns.entity.Furn;


import javax.print.DocFlavor;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.util.List;

/**
 * ClassName: FurnDAOImpl
 * Package: com.zxy.furns.dao.impl
 *
 * @Author zxy
 * @Create 2025/3/21 14:53
 * @Version 1.0
 * title:
 * Description:
 */
public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {
    @Override
    public List<Furn> queryFurns() {
        String sql = "SELECT id,name,maker,price,sales,stock,img_path imgPath FROM `furn`";
        return queryMulti(sql, Furn.class);
    }

    @Override
    public int addFurn(Furn furn) {
        String sql = "INSERT INTO furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`) VALUES(NULL,?,?,?,?,?,?)";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
    }

    @Override
    public int deleteFurn(Integer id) {
        String sql = "DELETE FROM furn WHERE id=?";
        return update(sql, id);
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` FROM furn WHERE id = ? ";
        return querySingle(sql, Furn.class, id);
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql = "UPDATE furn SET `name`=?,`maker`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? WHERE id=?";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath(), furn.getId());
    }

    @Override
    public int getTotalRow() {
        String sql = "SELECT COUNT(*) FROM furn";
        return ((Number) queryScalar(sql)).intValue();

    }

    @Override
    public List<Furn> getPageItems(int begin, int pageSize) {
        String sql = "SELECT id,name,maker,price,sales,stock,img_path imgPath FROM furn LIMIT ?,?";
        return queryMulti(sql, Furn.class, begin, pageSize);
    }

    @Override
    public int getPageTotalRowByName(String name) {
        String sql = "SELECT COUNT(*) FROM furn WHERE name LIKE ?";
        return ((Number) queryScalar(sql, "%" + name + "%")).intValue();
    }

    @Override
    public List<Furn> getPageItemByName(int begin, int pageSize, String name) {
        String sql = "SELECT id,name,maker,price,sales,stock,img_path imgPath FROM furn WHERE name LIKE ? LIMIT ?,? ";
        return queryMulti(sql, Furn.class, "%" + name + "%", begin, pageSize);
    }


}
